import {IndexModel} from "../../models/index";
const app = getApp()
const indexModel = new IndexModel()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    TabCur: 0,
    scrollLeft:0,
    tabList:['约拍','关注'],
    searching: false,
    hotWords:[],
    filterShow:false,
    params:{
      q:'',
      province:'全部',
      chargingModel:[],
      capacity:'',
      gender:'',
    },
    shootList:[],
    followList: [],
  },

  tabSelect(e) {
    const TabCur = e.currentTarget.dataset.id
    this.setData({
      TabCur: TabCur,
      scrollLeft: (e.currentTarget.dataset.id-1)*60,
    })
    this._changeTabOrReload(TabCur)
  },
  _changeTabOrReload(TabCur) {
    if (this._beforeLoad(TabCur) == 0) {
      return this._Load(0)
    }
  },
  _getHotList(topN) {
    indexModel.getHotWordList(topN).then(res=>{
      if (res.code == 0) {
        this.setData({
          hotWords:res.data
        })
      }
    })
  },
  //图片点击事件
  imgPreview:function(event){
    let src = event.currentTarget.dataset.src;//获取data-src
    let imgList = event.currentTarget.dataset.list;//获取data-list
    //图片预览
    wx.previewImage({
      current: src, // 当前显示图片的http链接
      urls: imgList // 需要预览的图片http链接列表
    })
  },
  handlerOnView: function (event) {
    let id = event.target.dataset.id
    wx.navigateTo({
      url: '/pages/index/details/about/index?id='+id
    })
  },
  bindShowFilter: function () {
      this.setData({
          filterShow: !this.data.filterShow
      })
  },

  handlerFilterSubmit: function (event) {
    this.setData({
      filterShow: false,
      params: event.detail.params,
    })
    this._Load(0)
  },
  handlerParentTap: function () {
      this.setData({
          filterShow: false
      })
  },
  handlerClickMask: function () {
      this.setData({
          filterShow: false
      })
  },
  onSearching: function() {
    this.setData({
      searching: true
    })
  },
  handlerOnConfirm: function (event) {
    let q = event.detail.q
    let params = this.data.params
    params.q = q
    this.setData({
      searching: false,
      params
    })
    this._Load(0)
  },
  handlerRegion: function (event) {
    let value = event.detail.addressName
    let params = this.data.params
    params.province = value
    this.setData({
      params,
    })
    this._Load(0)
  },
  onCancel: function(event) {
    let params = this.data.params
    params.q = ''
    this.setData({
      searching: false,
      params,
    })
    this._Load(0)
  },

  //下拉刷新
  onPullDownRefresh:function() {
    let _this = this
    wx.showNavigationBarLoading() //在标题栏中显示加载

    //模拟加载
    setTimeout(function()
    {
      // complete
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
      return _this._Load(0)
    },1500);
  },

  _beforeLoad(TabCur) {
    switch (TabCur) {
      case 0:
        return this.data.shootList.length
      case 1:
        return this.data.followList.length
      default:
        return false
    }
  },

  _Load(start,moreLoad) {
    wx.showLoading({
      title: '正在加载中...',
    })
    const tab = this.data.TabCur
    return tab == 0?this._LoadShootList(start,moreLoad):this._LoadShootFromIsFollow(start,moreLoad)
  },

  _LoadShootList(start,moreLoad) {
    indexModel.getShootList(this.data.params,start).then(res=>{
      if (typeof (moreLoad) == 'undefined') {
        this.setData({
          shootList:res.data
        })
      }else {
        this.setData({
          shootList:app._LoadMore(this.data.shootList,res.data)
        })
        this.unLoading(res.data)
      }
      wx.hideLoading();
    })
  },

  _LoadShootFromIsFollow(start,moreLoad) {
    indexModel.getIsFollowShootList(this.data.params,start).then(res=>{
      if (typeof (moreLoad) == 'undefined') {
        this.setData({
          followList:res.data
        })
      }else {
        this.setData({
          followList:app._LoadMore(this.data.followList,res.data)
        })
        this.unLoading(res.data)
      }
      wx.hideLoading()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this._Load(0)
    this._getHotList(10)
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let _this = this
    const TabCur = _this.data.TabCur
    _this._Load(_this._beforeLoad(TabCur),true)
  },

  unLoading(data) {
    if (data.length == 0) {
      app.showInfo('没有更多了哦~')
    }
  },

})
